   // ****************************************************** 
   //
   // Copyright 2011 OpenAjax Alliance
   //
   // Licensed under the Apache License, Version 2.0 (the "License");
   // you may not use this file except in compliance with the License.
   // You may obtain a copy of the License at
   //
   //    http://www.apache.org/licenses/LICENSE-2.0
   //
   // Unless required by applicable law or agreed to in writing, software
   // distributed under the License is distributed on an "AS IS" BASIS,
   // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   // See the License for the specific language governing permissions and
   // limitations under the License.
   //
   // ****************************************************** 
   

OAA_CI.links = OAA_CI.links || {}; 

/** ================================================================
*
* Links Module initialization
*
*
* =============================================================== */

OAA_CI.links_last_index = 0;

/** ================================================================
*
* onLoad
*
* @desc Initializes links module
*
* @return nothing
*
* =============================================================== */

OAA_CI.links.onLoad = function(oaa_cache_inspector) {
  
  OAA_CI.links_nls_strings = document.getElementById("ID_STRINGBUNDLE_LINKS");        
  
//  OpenAjax.a11y.console("Loaded link NLS strings: " + OAA_CI.links_nls_strings);
  
  OAA_CI.links.onShowLinksCommand();
}; 

/** ================================================================
*
* onShowLinksCommand
*
* @desc Evaluates the document and creates an EvaluationResults object
*
* @return nothing
*
* =============================================================== */

OAA_CI.links.onShowLinksCommand = function(e) {

  var i;
  var le;
  
  OAA_CI.evaluateDocument();   
  
  var links_cache = OAA_CI.evaluation.dom_cache.links_cache;  
  links_cache.sortLinkElements('document_order', true);
  var link_elements = links_cache.link_elements;
 
  var start_time = new Date().getTime();
  
  var xul_treechildren = document.getElementById('ID_TREECHILDREN_LINKS');
  
  OAA_CI.removeChildNodes(xul_treechildren);

  var link_elements_len = link_elements.length;
  
//  OpenAjax.a11y.console("Number of links: " + link_elements_len);

  if (link_elements_len) {
    for (i=0; i<link_elements_len; i++) {
      le = link_elements[i];
      OAA_CI.addTreeItem(xul_treechildren, [le.document_order, le.name, le.href], le.cache_id);
    }  // end loop
  }
  else {
    var xul_treeitem = OAA_CI.createXULElement('treeitem', []);
    OAA_CI.addTreeRow(xul_treeitem, ["none","",""], "none" );
    xul_treechildren.appendChild(xul_treeitem);
  }
  
  OAA_CI.updateXULTime(start_time);
    
};


/** ================================================================
*
* onLinkElementItemsSelectChanged
*
* @desc Updates properties tappanel with the information from the currently selected 
*       item in the LinkCache
*
* @return nothing
*
* =============================================================== */

OAA_CI.links.onLinkElementItemsSelectChanged = function (event) {

  var le;
  var col_id;
  var cache_id;
  
  var index = event.target.currentIndex;
  var links_cache = OAA_CI.evaluation.dom_cache.links_cache;  
  var xul_tree = document.getElementById('ID_TREE_LINKS');
  
  if (xul_tree) {
    col_id   = xul_tree.columns.getFirstColumn();
    cache_id = xul_tree.view.getCellValue(index, col_id);

    OpenAjax.a11y.console("Link cache id: " + cache_id );

    le = links_cache.getLinkElementByCacheId(cache_id);
    
    if (le) {
      OAA_CI.showCacheItemProperties(le);
    }
  } 
};

